package com.weight.system.equmtgrp.web;

import com.weight.system.equmtgrp.PO.EqumtgrpInfo;
import com.weight.system.equmtgrp.VO.EqumtgrpInfoVO;
import com.weight.system.equmtgrp.service.EqumtgrpInfoService;
import com.weight.system.sysoper.PO.SysOperInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.UserContext;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 设备管理表
 */
@RestController
@Slf4j
@RequestMapping("/equmtgrp")
public class EqumtgrpInfoController {

	@Autowired
	private EqumtgrpInfoService equmtgrpInfoService;

	private String pattern = "yyyy-MM-dd";


	/**
	 * 1.查询设备管理表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForEqumtgrpInfoList",method=RequestMethod.GET)
	public ModelAndView queryForEqumtgrpInfoList(){ 
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/equmtgrp/queryForEqumtgrpInfoList");
		Map<Object, Object> map =new HashMap<>();
		map.put("equmtgrpStatus", 1);
		SysOperInfo sysOperInfo =  UserContext.getCurreentSysOper();
		
		if(null == sysOperInfo) {
			return modelAndView;
		}
		map.put("equmtgrpAuth1", sysOperInfo.getOperId());
		map.put("equmtgrpAuth2", sysOperInfo.getOperId());
		map.put("equmtgrpAuth3", sysOperInfo.getOperId());

		
		//2、查询设备管理表列表
		List<EqumtgrpInfo> equmtgrpInfoList = equmtgrpInfoService.findEqumtgrpInfoByT(map);
		log.info("equmtgrpInfoList={}",equmtgrpInfoList);
		
		modelAndView.addObject("equmtgrpInfoVOList",  equmtgrpInfoList);
		return modelAndView;
	}

	/**
	 * 2.查询设备管理表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForEqumtgrpInfoListDG",method=RequestMethod.GET)
	public String queryForEqumtgrpInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String equmtgrpName = request.getParameter("equmtgrpName");
			String equmtgrpIp = request.getParameter("equmtgrpIp");
			String orgId = request.getParameter("orgId");
			String equmtgrpIntcode = request.getParameter("equmtgrpIntcode");
			String equmtgrpMsfcode = request.getParameter("equmtgrpMsfcode");
			String equmtgrpFlat = request.getParameter("equmtgrpFlat");
			String equmtgrpCount = request.getParameter("equmtgrpCount");
			String equmtgrpStatus = request.getParameter("equmtgrpStatus");
			String equmtgrppicPath = request.getParameter("equmtgrppicPath");
			String takecareStdate = request.getParameter("takecareStdate");
			String takecareCudate = request.getParameter("takecareCudate");
			String equmtgrpAuth1 = request.getParameter("equmtgrpAuth1");
			String equmtgrpAuth2 = request.getParameter("equmtgrpAuth2");
			String equmtgrpAuth3 = request.getParameter("equmtgrpAuth3");

			Map<Object, Object> map =new HashMap<>();
			map.put("equmtgrpName", equmtgrpName);
			map.put("equmtgrpIp", equmtgrpIp);
			map.put("orgId", orgId);
			map.put("equmtgrpIntcode", equmtgrpIntcode);
			map.put("equmtgrpMsfcode", equmtgrpMsfcode);
			map.put("equmtgrpFlat", equmtgrpFlat);
			map.put("equmtgrpCount", equmtgrpCount);
			map.put("equmtgrpStatus", equmtgrpStatus);
			map.put("equmtgrppicPath", equmtgrppicPath);
			map.put("takecareStdate", DateFormUtils.getStringToDate(takecareStdate, pattern));
			map.put("takecareCudate", DateFormUtils.getStringToDate(takecareCudate, pattern));
			map.put("equmtgrpAuth1", equmtgrpAuth1);
			map.put("equmtgrpAuth2", equmtgrpAuth2);
			map.put("equmtgrpAuth3", equmtgrpAuth3);

			 
			//2、查询设备管理表列表
			List<EqumtgrpInfo> equmtgrpInfoList = equmtgrpInfoService.findEqumtgrpInfoByPage(page,rows,map);
			log.info("equmtgrpInfoList={}",equmtgrpInfoList);
			List<EqumtgrpInfoVO> equmtgrpInfoVOList = equmtgrpInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<EqumtgrpInfoVO> result = new PageResult<EqumtgrpInfoVO>(equmtgrpInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param equmtgrpInfoVO
	 * @param equmtgrpInfo
	 * @return
	 */
	public void changeVOToPO(EqumtgrpInfoVO equmtgrpInfoVO,EqumtgrpInfo equmtgrpInfo){
			BeanUtils.copyProperties(equmtgrpInfoVO,equmtgrpInfo);
			equmtgrpInfo.setTakecareStdate(DateFormUtils.getStringToDate(equmtgrpInfoVO.getTakecareStdate(), pattern));
			equmtgrpInfo.setTakecareCudate(DateFormUtils.getStringToDate(equmtgrpInfoVO.getTakecareCudate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param equmtgrpInfoVO
	 * @param equmtgrpInfo
	 * @return
	 */
	public void changePoToVO(EqumtgrpInfoVO equmtgrpInfoVO,EqumtgrpInfo equmtgrpInfo){
			BeanUtils.copyProperties(equmtgrpInfo,equmtgrpInfoVO);
			equmtgrpInfoVO.setTakecareStdate(DateFormUtils.getDateToString(equmtgrpInfo.getTakecareStdate(), pattern));
			equmtgrpInfoVO.setTakecareCudate(DateFormUtils.getDateToString(equmtgrpInfo.getTakecareCudate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param equmtgrpInfo
	 * @return
	 */
	public EqumtgrpInfoVO changePoToVO(EqumtgrpInfo equmtgrpInfo){
			EqumtgrpInfoVO equmtgrpInfoVO=new EqumtgrpInfoVO();
			BeanUtils.copyProperties(equmtgrpInfo,equmtgrpInfoVO);
			equmtgrpInfoVO.setTakecareStdate(DateFormUtils.getDateToString(equmtgrpInfo.getTakecareStdate(), pattern));
			equmtgrpInfoVO.setTakecareCudate(DateFormUtils.getDateToString(equmtgrpInfo.getTakecareCudate(), pattern));
			

			return equmtgrpInfoVO;
	}

	/**
	 * 3.查询设备管理表--详情页
	 * @param equmtgrpInfoId
	 * @return
	 */
	@RequestMapping("/queryForEqumtgrpInfoDetail")
	public ModelAndView queryForEqumtgrpInfoDetail(@RequestParam(value="equmtgrpInfoId",required=false) String equmtgrpInfoId){
			//1.校验id
			CommonUtils.validateExistID(equmtgrpInfoId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("equmtgrpInfoId", equmtgrpInfoId);
			log.info("查询设备管理表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			EqumtgrpInfo equmtgrpInfo = equmtgrpInfoService.findByEqumtgrpInfoId(map);
			
			EqumtgrpInfoVO equmtgrpInfoVO = changePoToVO(equmtgrpInfo);
			//3、返回前台
			log.info("查询设备管理表查询成功：equmtgrpInfo={}",equmtgrpInfo);
			modelAndView.setViewName("equmtgrp/queryForEqumtgrpInfoDetail");
			modelAndView.addObject("equmtgrpInfoVO",equmtgrpInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 3.添加、修改设备管理表操作
	 * @param equmtgrpInfoId
	 * @return
	 */
	@RequestMapping(value="/addEqumtgrpInfo")
	public ModelAndView addEqumtgrpInfo(@RequestParam(value="equmtgrpInfoId",required=false) String equmtgrpInfoId){
			//1、创建返回对象
			EqumtgrpInfoVO equmtgrpInfoVO = new EqumtgrpInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(equmtgrpInfoId)) {
				Map<String, String> map=new HashMap<>();
				map.put("equmtgrpInfoId", equmtgrpInfoId);
				log.info("修改操作，查询ID：equmtgrpInfoId={}",equmtgrpInfoId);
				EqumtgrpInfo equmtgrpInfo = equmtgrpInfoService.findByEqumtgrpInfoId(map);
				this.changePoToVO(equmtgrpInfoVO,equmtgrpInfo);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("equmtgrp/addEqumtgrpInfo");
			modelAndView.addObject("equmtgrpInfoVO",equmtgrpInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改设备管理表操作
	 * @param equmtgrpInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveEqumtgrpInfo")
	public JsonResultUtil saveEqumtgrpInfo(@Valid EqumtgrpInfoVO equmtgrpInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,equmtgrpInfoVO);
			
			//2.获取id
			String equmtgrpInfoId=equmtgrpInfoVO.getEqumtgrpInfoId();
			EqumtgrpInfo equmtgrpInfo=new EqumtgrpInfo();
			BeanUtils.copyProperties(equmtgrpInfoVO,equmtgrpInfo);
			this.changeVOToPO(equmtgrpInfoVO,equmtgrpInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(equmtgrpInfoId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("equmtgrpInfoId", equmtgrpInfoId);
				log.info("修改操作，查询ID：equmtgrpInfoId={}",equmtgrpInfoId);
				//查询是否存在
				equmtgrpInfoService.findByEqumtgrpInfoId(map);
				try{
					equmtgrpInfo.setUpdateBy("000");
					equmtgrpInfoService.updateEqumtgrpInfo(equmtgrpInfo);
					log.info("更新操作成功：equmtgrpInfo={}",equmtgrpInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					equmtgrpInfo.setEqumtgrpInfoId(CommonUtils.getUUId());
					equmtgrpInfo.setCreateBy("000");
					equmtgrpInfo.setUpdateBy("000");
					
					SysOperInfo sysOperInfo =  UserContext.getCurreentSysOper();
					
					if(null == sysOperInfo) {
						return new JsonResultUtil(ResultEnum.NOT_LOGIN);
					}
					switch (sysOperInfo.getStandbyfield1()) {
					case "1":
						equmtgrpInfo.setEqumtgrpAuth1(sysOperInfo.getOperId());
						break;
					case "2":
						equmtgrpInfo.setEqumtgrpAuth2(sysOperInfo.getOperId());
						break;
					case "3":
						equmtgrpInfo.setEqumtgrpAuth3(sysOperInfo.getOperId());
						break;
					default:
						break;
					} 
					
					equmtgrpInfoService.insertEqumtgrpInfo(equmtgrpInfo);
					log.info("添加操作成功：equmtgrpInfo={}",equmtgrpInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 6.删除设备管理表操作
	 * @param equmtgrpInfoId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByEqumtgrpInfoId")
	public JsonResultUtil deleteByEqumtgrpInfoId(@RequestParam(value="equmtgrpInfoId",required=false) String equmtgrpInfoId){
			//1.校验id
			CommonUtils.validateExistID(equmtgrpInfoId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("equmtgrpInfoId", equmtgrpInfoId);
			equmtgrpInfoService.findByEqumtgrpInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除设备管理表入参，map={}",map);
			
			try{
				equmtgrpInfoService.upDelByEqumtgrpInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除设备管理表操作---批量删除
	 * @param equmtgrpInfoIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByEqumtgrpInfoIdBatch")
	public JsonResultUtil deleteByEqumtgrpInfoIdBatch(@RequestParam(value="equmtgrpInfoIdList",required=false) List equmtgrpInfoIdList){
			//1.校验id
			CommonUtils.validateExistIDList(equmtgrpInfoIdList);
			
			//2.校验数据库
			equmtgrpInfoService.findByEqumtgrpInfoIdList(equmtgrpInfoIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("equmtgrpInfoIdList", equmtgrpInfoIdList);
			map.put("updateBy","000");
			log.info("批量删除设备管理表操作入参，map={}",map);
			
			try{
				equmtgrpInfoService.upDelByEqumtgrpInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}